package com.authine.cloudpivot.ext.entity.order.extendsAttr;

import cn.hutool.core.lang.Console;
import com.alibaba.cola.exception.BizException;
import com.authine.cloudpivot.ext.utils.RegexUtils;
import com.authine.cloudpivot.ext.utils.kingdee.DateUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.IOException;
import java.io.Serializable;

/**
 * 订单商品扩展字段
 *
 * @author 张天赐
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class OrderGoodsExtendsAttr implements Serializable {

    private static final long serialVersionUID = 6372206516150938147L;

    /**
     * 补充澄清
     */
    private String clarifyTips;
    /**
     * 申购人
     */
    private String requisitioner;


    public static OrderGoodsExtendsAttr conversion(String value, boolean ifThrow) {
        OrderGoodsExtendsAttr result = OrderGoodsExtendsAttr.builder().build();
        ObjectMapper objectMapper = DateUtils.objectMapper();

        try {
            if (RegexUtils.notNull(value)) {
                result = objectMapper.readValue(value, OrderGoodsExtendsAttr.class);
            }
        } catch (IOException ex) {
            Console.error(ex);
            if (ifThrow) {
                throw new BizException("订单商品扩展属性字符串转换对象失败");
            }
        }

        return result;
    }

    public static String conversion(OrderGoodsExtendsAttr value, boolean ifThrow) {
        String result = null;
        ObjectMapper objectMapper = DateUtils.objectMapper();

        try {
            if (null != value) {
                result = objectMapper.writeValueAsString(value);
            }
        } catch (IOException ex) {
            Console.error(ex);
            if (ifThrow) {
                throw new BizException("订单商品扩展属性对象转换字符串失败");
            }
        }

        return result;
    }
}
